<%--
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
--%>
<%@ include file="/taglibs.jsp" %>
<s:layout-render name="/layout/normal.jsp">
  <s:layout-component name="htmlHead">
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/template.js"></script>
  </s:layout-component>
  <s:layout-component name="bodyElement"><body style="min-width:0; font-size: 0.8em"></s:layout-component>
  <s:layout-component name="header">

  </s:layout-component>
  <s:layout-component name="search">

  </s:layout-component>
  <s:layout-component name="body">
    <script type="text/javascript">
      function handleMessage(req) {
        eval(req.responseText);
      }

      function startClock(userId) {
        $('abtn_' + userId).disabled = true;
        $('clockAction').name = 'StartClock';
        document.clockForm.userId.value = userId;
        var params = Form.serialize($('clockForm'));
        new Ajax.Updater('result', $('clockForm').action, {method: 'post', postBody: params, onSuccess: handleMessage});
      }

      function stopClock(userId, entryId) {
        $('abtn_' + userId).disabled = true;
        $('clockAction').name = 'StopClock';
        document.clockForm.entryId.value = entryId;
        var params = Form.serialize($('clockForm'));
        new Ajax.Updater('result', $('clockForm').action, {method: 'post', postBody: params, onSuccess: handleMessage});
      }
    </script>
    <span id="result" style="display:none"></span>

    <div class="info-box">
      <h3>Time Clock</h3>
      <div id="actions" style="text-align:right">
      </div>
    </div>

    [<s:link beanclass="org.xptracker.web.EffortEntryActionBean">
      Manual Entry
      <s:param name="Edit" value="1"/>
      <s:param name="storyId" value="${actionBean.story.id}"/>
    </s:link>]
    <br/>
    <br/>
    <c:set var="priorComment" value="${empty actionBean.priorComment ? '' : actionBean.priorComment}"/>
    <c:if test="${!empty param.comment}">
      <c:set var="priorComment" value="${param.comment}"/>
    </c:if>
    
    Comment: <input type="text" size="40" value="${priorComment}"
                    onchange="$('commentField').value = this.value"/>
    <ul id="entries">
    </ul>

    <span id="actionWrapperTemplate" style="display:none">
      <span id="action_+{user.id}_wrapper"></span>
    </span>

    <span id="actionTemplate" style="display:none">
      <span id="action_+{user.id}">
        +{user.username}            
        <input id="abtn_+{user.id}" type="button" class="effort_+{display}" value="+{display}" onclick="+{script}"/> 
      </span><br/>
    </span>

    <span id="entryTemplate" style="display:none">
      <li id="entry_+{id}">+{start} - +{end} (+{user.username})</li>
    </span>

    <script type="text/javascript">
      var wrapperTemplate = TrimPath.parseTemplate($('actionWrapperTemplate').innerHTML)
      function addAction(script, display, user) {
        var result = wrapperTemplate.process({user: user});
        new Insertion.Bottom('actions', result);
        swapAction(script, display, user);
      }

      var entryTemplate = TrimPath.parseTemplate($('entryTemplate').innerHTML);
      function addEntry(entryId, user, start, end, comment) {
        var result = entryTemplate.process({id: entryId, user: user, start: start, end: end});
        new Insertion.Bottom('entries', result);
        new Insertion.Bottom('entries', comment);
      }

      var actionTemplate = TrimPath.parseTemplate($('actionTemplate').innerHTML);
      function swapAction(script, display, user) {
        var wrapper = $('action_' + user.id + '_wrapper');
        wrapper.innerHTML = actionTemplate.process({user: user, script: script, display:display});
      }
    </script>

    <c:forEach items="${actionBean.availableUsers}" var="user">
      <script type="text/javascript">
        addAction('startClock(${user.id})', 'Start', {id: '${user.id}', username: '${user.username}'});
      </script>
    </c:forEach>

    <c:forEach items="${actionBean.story.effortEntries}" var="entry">
      <c:if test="${!entry.completed}">
        <script type="text/javascript">
          addAction('stopClock(${entry.user.id}, ${entry.id})', 'Stop', {id: '${entry.user.id}', username: '${entry.user.username}'});
        </script>
      </c:if>
    </c:forEach>

    <c:forEach items="${actionBean.story.effortEntries}" var="entry">
      <c:if test="${entry.completed}">
        <fmt:formatDate value="${entry.start}" pattern="yyyy-MM-dd HH:mm" var="start"/>
        <fmt:formatDate value="${entry.end}" pattern="yyyy-MM-dd HH:mm" var="end"/>
        <script type="text/javascript">
          addEntry(${entry.id}, {id: '${entry.user.id}', username: '${entry.user.username}'}, '${start}', '${end}',
                   '${xp:jsFriendly(entry.comment)}');
        </script>
      </c:if>
    </c:forEach>

    <s:form name="clockForm" id="clockForm" beanclass="org.xptracker.web.EffortEntryActionBean" style="display:inline">
      <input type="hidden" id="clockAction" value="1"/>
      <input type="hidden" name="storyId" value="${actionBean.story.id}"/>
      <input type="hidden" name="userId"/>
      <input type="hidden" name="entryId"/>
      <input type="hidden" id="commentField" name="comment" value="${priorComment}"/>
    </s:form>
  </s:layout-component>
</s:layout-render>